﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace DacSanMienTayOBJ
{
    public class ImageProductBusiness
    {
        public static bool InsertImage(ImageProduct image, ConnectionType typeConnect)
        {
            Sqlhelper helper = new Sqlhelper(typeConnect);
            try
            {
                bool rs = false;
                string sql = "insert into imageproduct(MainImage,ThumbnailImage,IsMain) values(@mainImage,@thumbnailImage,@isMain)";
                SqlParameter[] pa = new SqlParameter[3];
                pa[0] = new SqlParameter("@mainImage",image.MainImage);
                pa[1] = new SqlParameter("@thumbnailImage",image.ThumbnailImage);
                pa[2] = new SqlParameter("@isMain",image.IsMain);
                SqlCommand command = helper.GetCommand(sql, pa, false);
                int row = command.ExecuteNonQuery();
                if (row > 0)
                {
                    rs = true;
                }
                return rs;
            }
            catch (Exception ex)
            {
                Utilitys.WriteLog(ex.Message);
                return false;
            }
            finally
            {
                helper.destroy();
            }
        }

        public static bool DeleteImage(int imageID, ConnectionType typeConnect)
        {
            Sqlhelper helper = new Sqlhelper(typeConnect);
            try
            {
                bool rs = false;
                string sql = "delete imageproduct where ImageID=@imageID";
                SqlParameter[] pa = new SqlParameter[1];
                pa[0] = new SqlParameter("@imageID",imageID);
               
                SqlCommand command = helper.GetCommand(sql, pa, false);
                int row = command.ExecuteNonQuery();
                if (row > 0)
                {
                    rs = true;
                }
                return rs;
            }
            catch (Exception ex)
            {
                Utilitys.WriteLog(ex.Message);
                return false;
            }
            finally
            {
                helper.destroy();
            }
        }

        public static bool UpdateImageIsMain(int imageID,int isMain, ConnectionType typeConnect)
        {
            Sqlhelper helper = new Sqlhelper(typeConnect);
            try
            {
                bool rs = false;
                string sql = "update imageproduct set IsMain=@isMain where ImageID=@imageID";
                SqlParameter[] pa = new SqlParameter[2];
                pa[0] = new SqlParameter("@imageID", imageID);
                pa[1] = new SqlParameter("@isMain", isMain);
                SqlCommand command = helper.GetCommand(sql, pa, false);
                int row = command.ExecuteNonQuery();
                if (row > 0)
                {
                    rs = true;
                }
                return rs;
            }
            catch (Exception ex)
            {
                Utilitys.WriteLog(ex.Message);
                return false;
            }
            finally
            {
                helper.destroy();
            }
        }

        public static ImageProduct GetImageProductDetail(int imageID, ConnectionType typeConnect)
        {
            Sqlhelper helper = new Sqlhelper(typeConnect);
            try
            {
                ImageProduct image = null;
                string sql = "select *from imageproduct where ImageID=@imageID";
                SqlParameter[] pa = new SqlParameter[1];
                pa[0] = new SqlParameter("@imageID",imageID);
                SqlCommand command = helper.GetCommand(sql, pa, false);
                SqlDataReader reader = command.ExecuteReader();
                if (reader.Read())
                {
                    image = new ImageProduct();
                    image.ImageID = int.Parse(reader["ImageID"].ToString());
                    image.MainImage = reader["MainImage"].ToString();
                    image.ThumbnailImage = reader["ThumbnailImage"].ToString();
                    image.IsMain = int.Parse(reader["IsMain"].ToString());
                }
                return image;
            }
            catch (Exception ex)
            {
                Utilitys.WriteLog(ex.Message);
                return null;
            }
            finally
            {
                helper.destroy();
            }
        }

        public static ImageProduct GetImageProductDetail(int imageID,int isMain, ConnectionType typeConnect)
        {
            Sqlhelper helper = new Sqlhelper(typeConnect);
            try
            {
                ImageProduct image = null;
                string sql = "select *from imageproduct where ImageID=@imageID and IsMain=@isMain";
                SqlParameter[] pa = new SqlParameter[2];
                pa[0] = new SqlParameter("@imageID", imageID);
                pa[1] = new SqlParameter("@isMain", isMain);
                SqlCommand command = helper.GetCommand(sql, pa, false);
                SqlDataReader reader = command.ExecuteReader();
                if (reader.Read())
                {
                    image = new ImageProduct();
                    image.ImageID = int.Parse(reader["ImageID"].ToString());
                    image.MainImage = reader["MainImage"].ToString();
                    image.ThumbnailImage = reader["ThumbnailImage"].ToString();
                    image.IsMain = int.Parse(reader["IsMain"].ToString());
                }
                return image;
            }
            catch (Exception ex)
            {
                Utilitys.WriteLog(ex.Message);
                return null;
            }
            finally
            {
                helper.destroy();
            }
        }

        public static bool InsertListProductImage(ProductImage productImage, ConnectionType typeConnect)
        {
            Sqlhelper helper = new Sqlhelper(typeConnect);
            try
            {
                bool rs = false;
                string sql = "insert into productimage(ProductID,ImageID) values(@productID,@imageID)";
                SqlParameter[] pa = new SqlParameter[2];
                pa[0] = new SqlParameter("@productID", productImage.ProductID);
                pa[1] = new SqlParameter("@imageID", productImage.ImageID);
                SqlCommand command = helper.GetCommand(sql, pa, false);
                int row = command.ExecuteNonQuery();
                if (row > 0)
                {
                    rs = true;
                }
                return rs;
            }
            catch (Exception ex)
            {
                Utilitys.WriteLog(ex.Message);
                return false;
            }
            finally
            {
                helper.destroy();
            }
        }

        public static bool DeleteListProductImageFollowProduct(int productID, ConnectionType typeConnect)
        {
            Sqlhelper helper = new Sqlhelper(typeConnect);
            try
            {
                bool rs = false;
                string sql = "delete productimage where ProductID=@productID";
                SqlParameter[] pa = new SqlParameter[1];
                pa[0] = new SqlParameter("@productID", productID);
                SqlCommand command = helper.GetCommand(sql, pa, false);
                int row = command.ExecuteNonQuery();
                if (row > 0)
                {
                    rs = true;
                }
                return rs;
            }
            catch (Exception ex)
            {
                Utilitys.WriteLog(ex.Message);
                return false;
            }
            finally
            {
                helper.destroy();
            }
        }

        public static bool DeleteListProductImageFollowImage(int imageID, ConnectionType typeConnect)
        {
            Sqlhelper helper = new Sqlhelper(typeConnect);
            try
            {
                bool rs = false;
                string sql = "delete productimage where ImageID=@imageID";
                SqlParameter[] pa = new SqlParameter[1];
                pa[0] = new SqlParameter("@imageID", imageID);
                SqlCommand command = helper.GetCommand(sql, pa, false);
                int row = command.ExecuteNonQuery();
                if (row > 0)
                {
                    rs = true;
                }
                return rs;
            }
            catch (Exception ex)
            {
                Utilitys.WriteLog(ex.Message);
                return false;
            }
            finally
            {
                helper.destroy();
            }
        }

        public static List<int> ListImageIDFollowProduct(int productID, ConnectionType typeConnect)
        {
            Sqlhelper helper = new Sqlhelper(typeConnect);
            try
            {
                List<int> list = new List<int>();
                string sql = "select ImageID from productimage where ProductID=@productID";
                SqlParameter[] pa = new SqlParameter[1];
                pa[0] = new SqlParameter("@productID", productID);
                SqlCommand command = helper.GetCommand(sql, pa, false);
                SqlDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    list.Add(int.Parse(reader["ImageID"].ToString()));
                }
                return list;
            }
            catch (Exception ex)
            {
                Utilitys.WriteLog(ex.Message);
                return null;
            }
            finally
            {
                helper.destroy();
            }
        }
    }
}
